# coding=utf-8
import requests
from lxml from etree
from multiprocessing.dummy import Pool as ThreadPool
import json

url = 'http://tieba.baidu.com/p/3522395718'


def towrite(contentdict):
    f.writeLines(u'回帖时间：' + str(contentdict['topic_reply_time']) + '\n')
    f.writeLines(u'回帖内容：' + unicode(contentdict['topic_reply_content']) + '\n')
    f.writeLines(u'回帖作者：' + contentdict['user_name'] + '\n\n')


def spilder(url):
    html = requests.get(url)
    selector = etree.HTML(html.text)
    content_field=selector.xpath('//div[@class="l_post j_l_post l_post_bright  "]')
    item={}
    for each in content_field:
        reply_info=json.loads(each.xpath('@data-field')[0].replace('&quot',''))
        author=reply_info['author']['user_name']
        content=each.xpath('div[@class="d_post_content_main"]/div/cc/div[@class="d_post_content j_d_post_content  clearfix"]/text()')
        reply_time=reply_info['content']['date']
        print(content)
        print(reply_time)
        print(author)
        item['user_name']=author
        item['opic_reply_content'] = content
        item['topic_reply_time'] = reply_time
        towrite(item)

if __name__=='__main__':
    pool=ThreadPool(4)
    f=open('content.txt','a')
    page=[]
    for i in range(1,2):
        newpage=url+'?pn='+str(i)
        page.append(newpage)
    results=pool.map(spilder,page)
    pool.close()
    pool.join()
    f.close()